package prv.pursuivant.render.html;

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import java.io.StringWriter;
import java.util.Date;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.internal.util.MockUtil;

import prv.pursuivant.core.AchievType;
import prv.pursuivant.core.AchievTypesTree;
import prv.pursuivant.core.AchievTypesTreeImpl;
import prv.pursuivant.core.impl.CategoryAchievType;
import prv.pursuivant.core.impl.NormalAchievType;
import prv.pursuivant.core.impl.NpcAchievType;
import prv.pursuivant.render.Destination;
import prv.pursuivant.util.DateUtil;

import com.google.inject.internal.Lists;

public class HtmlPageDerorationRendererTest {

	private AchievType naxx;
	private AchievTypesTree tree;
	private AchievType q1anub;
	private AchievType q1;
	private DateUtil dateUtil;
	private Date testWhen;
	private CategoryAchievType root;
	private StringWriter writer;
	private XHtmlGenerator generator;
	private XMLStreamWriter xmlWriter;

	@Before
	public void setUp() throws XMLStreamException {
		q1anub = new NpcAchievType("Anub'Rekhan", "562");
		AchievType q1b = new NpcAchievType("Maexxna", "562");
		AchievType q1c = new NpcAchievType("Grand Widow Faerlina", "562");
		q1 = new NormalAchievType("The Arachnid Quarter", "562", Lists
				.newArrayList(q1anub.getId(), q1b.getId(), q1c.getId()));
		naxx = new NormalAchievType("The Fall of Naxxramas", "576", Lists
				.newArrayList(q1.getId()));
		root = new CategoryAchievType("Dungeons & Raids", "168", Lists
				.newArrayList(naxx.getId()));
		tree = new AchievTypesTreeImpl(Lists.newArrayList(naxx, q1, q1anub,
				q1b, q1c), root);
		dateUtil = new DateUtil();
		testWhen = dateUtil.createDate(2008, 12, 13);

		writer = new StringWriter();
		xmlWriter = XMLOutputFactory.newInstance()
				.createXMLStreamWriter(writer);
		generator = new XHtmlGenerator(xmlWriter);

	}

	@Test
	public void shouldRenderAchievList() {
		
		Destination dest = mock(Destination.class);
		when(dest.getUriForPage("168")).thenReturn("output.168.html");
		when(dest.getUriForPage("562")).thenReturn("output.562.html");
		
		HtmlPageDecorationRenderer renderer = new HtmlPageDecorationRenderer(
				tree, Lists.newArrayList("168","576","562"),dest);
//		String actual = renderer.renderAchievTree(naxx);
		renderer.renderAchievList(generator, naxx);
		String actual = writer.toString();
		String expected = 
				"Categories: &bull; <a href=\"output.168.html\">Dungeons &amp; Raids</a> (1)<br/>" +
				"Other: &bull; The Fall of Naxxramas (1) &bull; " +
				"<a href=\"output.562.html\">The Arachnid Quarter</a> (3)<br/>";
		assertEquals(expected, actual);
	}

}
